Cn_Act ={
	DATA : {}
	,viewInit : function(){
		if(!Jes.userSess(1)) return;//session
		Jes('users').value = '';//选中数据的清空
		var actid = Jes.request('id');
		//var type = Jes.request('type');//tab页的查询
		if(!actid) return;
		Jes('actid').value = actid;
		Jes.query('cnactview_query_sql@act',{paraObj:"{id:'"+actid+"'}"},function(data){
			data= data[0];
			Cn_Act.DATA['isCU'] = userInfo.userid == data.userid;//判断当前用户是否创建人
			//Jes.html('content',data.content);
			Jes.setFormValue('form1',data);
			Jes.html('content',data.content);
			Jes.html('acttime',data.acttime);
			Jes.html('hour',data.acthour+':00:00');
			if(data.busname) Jes.html('_busname','  <a href="../bus/busview.html?id='+data.busid+'" target="blank">'+data.busname+'</a>');
			Jes.html('pic','<img id="hdpic" src="'+CONFIG_DATA.path+data.actpic+'" width="365" height="258"/>');
			Jes.html('factnum',data.factnum+'人参加');
			var feetype='';
			if(data.feetype=='0'){feetype='免费'}
			if(data.feetype=='1'){feetype='发起人请客'}
			if(data.feetype=='2'){feetype='AA制'}
			var intro = '';
			if(data.money==null||data.money==''){//不花钱的活动
				Jes.html('money','0');
			}else{
				Jes.htm
				Jes.html('money',data.money);
				intro = ',人均'+data.money+'(仅参考),';
			}
			var lastnum = (data.maxnum || 100)-data.factnum;
			Jes.html('mun',lastnum);
			Jes.html('hidepl',  '活动时间：'+ data.acttime + '  '+ data.acthour +'<br/>活动地点：'+ data.address +'<br/>付费性质：'
			 	 + feetype +'<br/>活动内容：'+data.remark);//活动内容
			Jes('topicid').value = data.topicid;
			//加一步判断我的状态
			Cn_Act.actMan(actid);
			Cn_Act.actPlun(actid);
			Cn_Act.iniMap(data.address);
		});
	}
	//活动图片初始化
	,viewpicinit : function(){
		var picid = Jes.request('picid');
		Cn_Act.viewpic(picid);
		Cn_Act.picpl(picid);
	}
	,viewpic : function(picid){
		Jes.query('viewpic_query_sql@act',{paraObj:"{id:'"+picid+"'}",'isSess':0,isLoad:false},function(data){
			for(var i=0;i<data.length;i++){
				if(data[i].id==picid){
					Jes('backview').href="actview.html?id="+data[i].actid;
					if(i==0){
						Jes('pre_photo').href="viewpic.html?picid="+data[data.length-1].id;
					}else{
						Jes('pre_photo').href="viewpic.html?picid="+data[i-1].id;
					}
					if(i==data.length-1){
						Jes('next_photo').href="viewpic.html?picid="+data[0].id;
						Jes('mainphoto').href="viewpic.html?picid="+data[0].id;
					}else{
						Jes('next_photo').href="viewpic.html?picid="+data[i+1].id;
						Jes('mainphoto').href="viewpic.html?picid="+data[i+1].id;
					}
					Jes('maingimg').src=CONFIG_DATA.actpath+data[i].pic;
					var html = '来自<a href="../front/person.html?id='+data[i].userid+'">'+data[i].username+'</a>'
						+'上传于&nbsp;'+data[i].createtime+'&nbsp;&nbsp;'
					    +'<span class="rec" id="None-None">'
					    +'分享到&nbsp;&nbsp;'
					    +'<a class="s_tx" href="javascript:void(0);" title="腾讯微博" onclick="postToQQ(); return false;">&nbsp;&nbsp;</a>'
						+'<a class="s_sina" href="javascript:void(0);" title="新浪微博" onclick="postToSina();return false;">&nbsp;&nbsp;</a>'
					    +'</span>';
					Jes.html('picinfo',html);
				}
			}
		});	
	}
	,picpl : function(picid){
		Jes.query('actpicpl_query_sql@act',{paraObj:"{picid:'"+picid+"'}",'isSess':0,isLoad:false},function(data){
			var html = [];
			for(var i=0;i<data.length;i++){
				html.push(Comm._actPlunHtml(data[i],i));
				Jes('topicid').value=data[i].topicid;
			}
			Jes.html('commentscontent',html);
		});
	}
	,doPicPlun : function(){
		var topicid = Jes('topicid').value;
		alert(topicid);
		if(topicid=='')return;
		var pl = Jes('plContent'),plVal=pl.value,plzf = Jes('plzf'),rid=Jes('replyid').value;
		if(plVal==''){
			Jes.alert('评论内容不能为空!');
			pl.focus();
			return;
		}
		
		Jes.doSaveTObject('CnComment',{param:{'topicid':topicid,content:plVal,reuserid:rid,iszf:plzf.checked?'1':'0'}},function(data){
			Cn_Act.picpl(Jes.request('picid'));
		});
		
	}
	//活动成员 actid 活动id  type tab页
	,actMan : function(actid){
		Jes.query('cnactjoinuser_query_sql@act',{paraObj:"{actid:'"+actid+"'}",isLoad:false},function(data){
			var join_user =[],ask_user=[],invite_user=[],jnum=0,anum=0,inum=0,imgsrc,loginid,userid,isCU=false;
			Jes('alluserids').value='';
			Jes.each(data,function(i){
				imgsrc=Jes.getPicPath(data[i].pic),loginid=data[i].loginid;userid=data[i].userid;
				if(userInfo.userid == data[i].userid) Cn_Act.DATA['status'] = data[i].status;
				if(data[i].status=='2'){
					jnum++;
					join_user.push('<li onmouseover="Cn_Act.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Cn_Act.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'"/></a><p>'+loginid+'</p>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
				}else if(data[i].status=='1'){
					if(Cn_Act.DATA['isCU']){
						anum++;
						ask_user.push('<li id="askli'+userid+'"  onmouseover="Cn_Act.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Cn_Act.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a onclick="Cn_Act.seleted(\''+userid+'\')" class="choose"><img src= "'+imgsrc+'" /><span  class="chopic"></span></a>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
                    	Jes('alluserids').value = Jes('alluserids').value + userid + ',';
					}else{
						anum++;
						ask_user.push('<li id="askli'+userid+'"  onmouseover="Cn_Act.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Cn_Act.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'" /></a><p>'+loginid+'</p>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
					}
				}else if(data[i].status=='0'){
					inum++;
					invite_user.push('<li onmouseover="Cn_Act.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Cn_Act.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'"/><span></span></a>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
				}
				
			});
			Cn_Act.showOp();
			Jes.html('join_user',join_user),Jes.html('ask_user',ask_user),Jes.html('invite_user',invite_user);
			Jes.html('jnum','('+jnum+'人)'),Jes.html('anum','('+anum+'人)'),Jes.html('inum','('+inum+'人)');
		});
	}
	//初始化评论部分
	,actPlun : function(actid){
		Jes.query('actpl_query_sql@act',{paraObj:"{actid:'"+actid+"'}",isLoad:false},function(data){
			var html=[];
			Jes.each(data,function(i){
			html.push('<li>');
              html.push('<div class="commentsTxt"> <a href="#" title="'+data[i].loginid+'" class="comimg"><img width="40" height="40" class="picborder_l" title="'+data[i].loginid+'" src="'+Jes.getPicPath(data[i].pic)+'"/></a>');
                html.push('<div class="commentsContants">');
                  html.push('<p class="commentsParm"><a href="#" class="textname">'+data[i].loginid+'</a> :'+data[i].content+'('+data[i].ctime+') </p>');
                  html.push('<div class="commentOption">');
                  if(data[i].fqr==userInfo.userid||data[i].userid==userInfo.userid){//活动发起人 才能删除
                  	html.push('<p class="rt"><a  href="javascript:void(0);" onclick="Cn_Act.delActPlun(this,\''+data[i].id+'\',\''+data[i].topicid+'\')">删除</a> </p>');
                  }
                  html.push('</div>');
                  html.push('<div class="clearit"></div>');
                html.push('</div>');
              html.push('</div>');
            html.push('</li>');
			});	
			Jes.html('commentscontent',html);
		});
	}
	//活动地图显示
	,iniMap : function(address){
      	this.map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 15,
            center: new google.maps.LatLng(30.274090, 120.155067), // 初始中心点杭州
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        if(address!=null&&address!=''){
       		var map = this.map;
			this.geocoder = new google.maps.Geocoder();
        	this.geocoder.geocode({address: address}, function(results, status) {
            	if (status == google.maps.GeocoderStatus.OK) {
               	 var latlng = results[0].geometry.location;
                	map.setCenter(latlng);
               	 var marker = new google.maps.Marker({
                  	  title: '活动地点：'+address,//$('name').value,
                 	   map: map,
                 	   position: latlng
               	 });
            	}
        	});        
        }
	}
	//评论的删除
	,delActPlun : function(obj,commentId,topicid){
		Jes.executeSql({ids:commentId,topicid:topicid,sql:'cncomment_delete_hql'},function(){
			var el = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
			el.parentNode.removeChild(el);
		});
	}
	//发评论
	,actDoPlun : function(){
		var actid = Jes('actid').value;
		var topicid = Jes('topicid').value;
		var content = Jes('plContent').value;
		if(actid==null||actid==''){return;}
		if(content.length==0){ alert('请先填写评论内容');return;}
		if(topicid!=null&&topicid!=''){
			var plzf = Jes('plzf');
			Jes.doSaveTObject('CnComment',{param:{'topicid':topicid,content:content,iszf:plzf.checked?'1':'0'}},function(data){
				Jes('plContent').value.value='';
				plzf.checked=false;
			//	Cn_Act.showPlun(id); 做成动态的加载li 不要刷新
				Cn_Act.viewInit();
			});
		}
	}
	//全选的事件
	,allSeleted : function(obj){
		var allusers = Jes('alluserids').value.split(',');
		if(obj.checked){
			for(var i=0;i<allusers.length-1;i++){
				if($('#askli'+allusers[i]).find('a').hasClass('onchoose')){
					null;
				}else{
					Jes('users').value=Jes('users').value + allusers[i] + ',';
					$('#askli'+allusers[i]).find('a').addClass('onchoose');
				}
			}
		}else{
			for(var i=0;i<allusers.length-1;i++){
			if($('#askli'+allusers[i]).find('a').hasClass('onchoose')){
				$('#askli'+allusers[i]).find('a').removeClass('onchoose');
				Jes('users').value=Jes('users').value.replace(allusers[i]+',','');
			}else{
				null;
			}
			}
		}
	}
	//评论成功后展示
	,showPlun : function(){
		
		
		
	}
	//选择申请人
	,seleted : function(div){
		if($('#askli'+div).find('a').hasClass('onchoose')){
			$('#askli'+div).find('a').removeClass('onchoose');
			Jes('users').value=Jes('users').value.replace(div+',','');
		}else{
			Jes('users').value=Jes('users').value+div + ',';
			$('#askli'+div).find('a').addClass('onchoose');
		}
	}
	//显示tab
	,showTab : function(tab){
		for(var i=1;i<4;i++){
			if(i!=tab) {
				$('#tab1'+i).removeClass('tabseled');
				document.getElementById('tdivs1'+i).style.display ='none';
			}
		}
		if(tab=='2'&&Cn_Act.DATA['isCU']){
			document.getElementById('titlerb').style.display ='block';
		}else{
			document.getElementById('titlerb').style.display ='none';
		}
		$('#tab1'+tab).addClass('tabseled');
		document.getElementById('tdivs1'+tab).style.display ='block';
		
	}
	,showOp : function(){
		if(Cn_Act.DATA['status']){
			Jes.hide('s');
			Jes.show('s'+Cn_Act.DATA['status']);
		}else{
			Jes.show('s');
		}
	}
	,doJoinAct : function(isag,userid){
		var actid = Jes.request('id');
		//if(userid.length==0){alert('请先选择用户');return;}
		var info = '你确定加入此活动吗？';
		if(isag==1) info='确定通过吗？';
		else if(isag ==2) info='确定拒绝吗？';
		if(!confirm(info)) return;
		Jes.doSaveTObject('cn.joinact',{param:{'actid':actid,'isag':isag,'userid':userid}},function(data){
			Cn_Act.viewInit();//优化
			alert(data);
		});
	}
}
Cn_ActDeal ={
	Init : function(){
		if(!Jes.userSess(1))return;
		var actid = Jes.request('id');
		Jes.setToday('today');
		//活动信息
		Jes.query('cnactview_query_sql@act',{paraObj:"{id:'"+actid+"'}",'isSess':0,isLoad:true},function(data){
			if(data.length==0){
				history.go(-1);
				return;
			}
			var data = data[0];
			var html = Cn_ActDeal._actDealHtml(data);
			Jes.html('actionfo',html);
		//	Jes.setFormValue("form1",data);
			Jes('actid').value = actid;
			Jes('busid').value = data.busid;
			Jes('price').value = data.money;
			Jes('content').value = data.content;
			Jes('createrid').value = data.userid;
			Jes.html('dealinfo', '活动主题:“'+data.content+"”,费用"+data.money+'元');
			var balance,total;
			Jes('personnum').value = 1;
			Jes.doQueryList('getmybalance_query_sql@user',{paraObj:'{type:2}',isLoad:false},function(data){
				balance = data[0].balance;
				Jes('balance').value = balance;
				//显示付款方式
				Cn_ActDeal.showPaytype(1,Jes('price').value,balance);
			});
		})
		Jes.syn(userInfo.userid);
		if(!Jes.userSess(1))return;
		if(!userInfo.phone){
			Jes.showDiv('#showDiv6');
		}else{
			Jes.html('phone',userInfo.phone);
		}
	}
	//显示付款方式
	,showPaytype : function(personnum,price,balance){
		Jes('person').value = personnum;
		Jes.html('userbalance','¥'+balance);
		Jes.html('money','¥'+price);
		var total = accMul(personnum,price);
		Jes.html('total','¥'+total);
		Jes.html('dealtotal','¥'+total);
		
		Jes('price').value = price;
		Jes('totalfee').value= total;
		Jes('personnum').value = personnum;
		if(balance==0){
			Jes.hide('div_balance');
			Jes.hide('div_cnpay');
			Jes.hide('div_cnthird');
			Jes.show('order-check-typelist');
			Jes('cnfee').value = 0;
			Jes('thirdfee').value = total;
		}else{
			if(balance >= total){
				Jes.show('div_balance');
				Jes.show('div_cnpay');
				Jes.hide('div_cnthird');
				Jes.hide('order-check-typelist');
				Jes('cnfee').value = total;
				Jes('thirdfee').value = 0;
			}
			if(balance < total){
				Jes.html('mybalance',balance);
				Jes.html('mythird',subtr(total,balance));
				Jes.hide('div_cnpay');
				Jes.show('div_balance');
				Jes.show('div_cnthird');
				Jes.show('order-check-typelist');
				this.checkPay(balance,total);
			}
		}
	}
	,checkPay : function(balance,total){
		var balance = Jes('balance').value;
		var total = Jes('totalfee').value;
				if(Jes('checkpay').checked){
					Jes('cnfee').value = balance;
					Jes('thirdfee').value = total-balance;
				}else{
					Jes('cnfee').value = 0;
					Jes('thirdfee').value = total;
				}
	}
	,checkClick : function(el){
		Jes.click(el);
		this.checkPay();
	}
	//保存手机号码
	,savePhone : function(){
		if(Jes('userphone').value.length!=11){
			Jes.alert('手机位数不对');
			return;
		}else{//保存手机号码再加入活动
			Jes.executeSql({ids:'1',phone:Jes('userphone').value,userid:userInfo.userid,sql:'savephone_update_hql'},function(){
				$('#showDiv6').hide();
				Jes.html('phone',Jes('userphone').value);
			});
		}
	}
	//提交订单信息
	,submitDeal : function(){
		var inParam = DWRExtUtil.getFormValue("form1",1);
		inParam.phone = Jes('phone').innerHTML;
		if(inParam.phone==null||inParam.phone==''){
			alert('手机号码不准为空');
			return;
		}
		inParam.userid= userInfo.userid;
		inParam.actid = Jes('actid').value;
		inParam.busid = Jes('busid').value;
		inParam.money = Jes('money').value;
		inParam.dealinfo = Jes('dealinfo').innerHTML;
		inParam.content = Jes('content').value;
		inParam.sellermail = userInfo.email;
		inParam.personnum = Jes('personnum').value;
		inParam.price = Jes('price').value;
		inParam.totalfee = Jes('totalfee').value;
		inParam.cnfee = Jes('cnfee').value;
		inParam.thirdfee = Jes('thirdfee').value;
		inParam.status = '0';//默认
		inParam.createrid = Jes('createrid').value;
		var temp = document.getElementsByName("paytype");
	    for(var i=0;i<temp.length;i++){
	   		if(temp[i].checked)
	        inParam.paytype = temp[i].value;
	    }
	    var rsl;
	    Jes.doSync(function(){Jes.save('CnDeallog',{param:inParam,isLoad:false},function(data){rsl = data;});});
	   	if(rsl!=null&&rsl!=''&&rsl.indexOf('alipay')!=-1){
			Jes.openLayer('actdealbtn','showpay');
			//window.open('temp.html?id='+rsl)
			window.open(encodeURI(rsl));
		}else if(rsl!=null&&rsl!=''&&rsl=='1'){// banacle  to pay
			window.location.href="actview.html?id="+Jes.request('id');
			alert('完成购买,簇鸟密码已经发送到您手机。');
		}
		/*
		Jes.save('CnDeallog',{param:inParam},function(data){
			//	window.open('temp.html?id='+data+'&paytype='+inParam.paytype);
			//window.open(encodeURI(data));
			if(data!=null&&data!=''&&data.indexOf('alipay')!=-1){
				Jes.openLayer('actdealbtn','showpay');
				window.open('temp.html?id='+data)
				//window.open(encodeURI(data));
			}else if(data!=null&&data!=''&&data=='1'){// banacle  to pay
				window.location.href="actview.html?id="+Jes.request('id');
				alert('完成购买,簇鸟密码已经发送到您手机。');
			}
		});
		*/
	}
	//actdeal.html  initHtml
	,_actDealHtml : function(data){
		var actinfoHtml = '<a href="#" class="aimg"><img id="actpic" width="100" height="80" src="'+ CONFIG_DATA.path+data.actpic +'"/></a>'
                +'<div class="order_text">'
                	+'<div class="sbggrg"> '
                     +'<span class="name">'+data.content+'</span>'
                     +'<p style="font-size:14px;">活动时间：'+ Cn_ActDeal.acttimeFormat(data.acttime,data.acthour,data.endtime,data.endhour) +'</p>'
                    +'<p style="font-size:14px;">参加人数：'+data.factnum+'/'+data.maxnum+'</p>'
                   +'</div>'
                +'</div>';
		return actinfoHtml;
	}
	//return actimeformat
	,acttimeFormat : function(acttime,acthour,endtime,endhour){
		if(acttime==endtime){
			var time = Jes.getDateStrMoonDay(acttime);
			var week = Jes.getTimeWeekStr(acttime);
			return time + ' '+ week +' ' +acthour+' - '+endhour;
		}else{
			var stime = Jes.getDateStrMoonDay(acttime);
			var etime = Jes.getDateStrMoonDay(endtime);
			var actweek = Jes.getTimeWeekStr(acttime);
			var endweek = Jes.getTimeWeekStr(endtime);
			return stime + ' '+ actweek +' - ' +etime+' '+endweek;
		}
	}
	//改变人数
	,changeNum : function(num){
		try{
			if (/[^\d]/.test(num)){alert('请输入数字');return;}
			if(num.length==0||num==0)return;
			this.showPaytype(num,Jes('price').value,Jes('balance').value)
		}catch(e){
			alert('人数填写错误');
			this.showPaytype(1,Jes('price').value,Jes('balance').value)
		}
	}
	,gotoview : function(){
		window.location.href="actview.html?id="+Jes.request('id');
	}
	//用户支付后返回
	,userPayReturn : function(flag){
		var actid = Jes('actid').value;
		var userid = userInfo.userid;
		Jes.doQueryList('userpayreturn_query_sql@act',{paraObj:'{actid:\''+actid+'\',userid:\''+userid+'\'}'},function(data){
			if(data==null||data==''){
				location.reload();
			}else{
				window.location.href='actview.html?id='+actid;
			}
		});
	}
}